package com.summer.campussct.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.campussct.mapper.CampusOrShopMapper;
import com.summer.campussct.pojo.CampusOrShop;
import com.summer.campussct.pojo.dto.CampusOrShopDto;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.service.ICampusOrShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CampusOrShopServiceImpl extends ServiceImpl<CampusOrShopMapper, CampusOrShop> implements ICampusOrShopService {

    @Resource
    private CampusOrShopMapper campusOrShopMapper;

    @Override
    public RestResult listWithVehicles(int type) {
        //1. 查询每个学校或店铺的id
        List<Integer> idList = campusOrShopMapper.idsByType(type);

        //2. 根据id查询其关联的一页车辆，存入集合中
        ArrayList<CampusOrShopDto> campusOrShopDtoList = new ArrayList<>(10);
        for (Integer id : idList) {
            CampusOrShopDto campusOrShopDto = campusOrShopMapper.getOneWithVehicles(id);
            // 这一步判断很重要，不然列表就会加入null
            if (campusOrShopDto != null){
                //3. 查询车源数和今日上新数
                Integer count = campusOrShopMapper.getVehicleCountById(id);
                Integer todayCount = campusOrShopMapper.getVehicleTodayCountById(id);
                campusOrShopDto.setCount(count);
                campusOrShopDto.setTodayCount(todayCount);
                campusOrShopDtoList.add(campusOrShopDto);
            }
        }

        return RestResult.success(campusOrShopDtoList);
    }

    @Override
    public CampusOrShopDto getDescById(int id) {
        CampusOrShopDto campusOrShopDto = new CampusOrShopDto();
        CampusOrShop campusOrShop = this.getOne(new LambdaQueryWrapper<CampusOrShop>()
                .eq(CampusOrShop::getId, id)
                .select(CampusOrShop::getId,CampusOrShop::getName, CampusOrShop::getPicture, CampusOrShop::getType, CampusOrShop::getAddr));
        if (campusOrShop != null){
            BeanUtil.copyProperties(campusOrShop,campusOrShopDto);
            Integer count = campusOrShopMapper.getVehicleCountById(id);
            Integer todayCount = campusOrShopMapper.getVehicleTodayCountById(id);
            campusOrShopDto.setCount(count);
            campusOrShopDto.setTodayCount(todayCount);
        }
        return campusOrShopDto;
    }

    @Override
    public RestResult getCampusList() {
        return RestResult.success(this.list(new LambdaQueryWrapper<CampusOrShop>()
                .select(CampusOrShop::getId, CampusOrShop::getName)
                .eq(CampusOrShop::getType, 1)));
    }

    @Override
    public RestResult getNameById(int id) {
        CampusOrShop c = getOne(new LambdaQueryWrapper<CampusOrShop>()
                .select(CampusOrShop::getName)
                .eq(CampusOrShop::getId, id));
        return RestResult.success(c.getName());
    }
}
